package com.wythe.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.wythe.mall.R;
import com.wythe.mall.adapter.ImageShopAdapter;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.ShopBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.SharePreferenceUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import top.yokey.base.bean.GoodsBean;

public class ListActivity extends BaseActivity implements View.OnClickListener {

    private EditText searchEditText;
    private AppCompatTextView orderTextView;
    private AppCompatTextView saleTextView;
    private AppCompatTextView screenTextView;
    private LinearLayoutCompat orderLinearLayout;
    private RelativeLayout screenRelativeLayout;
    private AppCompatTextView orderCompTextView;
    private AppCompatTextView orderHighTextView;
    private AppCompatTextView orderLowTextView;
    private AppCompatTextView orderHotTextView;
    private AppCompatEditText priceFromEditText;
    private AppCompatEditText priceToEditText;
    private AppCompatTextView confirmTextView;
    private GridView mainPullRefreshView;

    private String bIdString;
    private String gcIdString;
    private String keywordString;

    private String keyString;
    private String orderString;
    private boolean isGridModel;
    private String priceToString;
    private String priceFromString;

    private int pageInt;
    private ArrayList<ShopBean> mainArrayList;
    private ImageShopAdapter mAdapter;
    private String keyWord;
    private int sign = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        if (mainArrayList == null) {
            mainArrayList = new ArrayList<>();
        }
        keyWord = getIntent().getExtras().getString("keyword");
        initView();
        initData();
    }

    public void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ((TextView) findViewById(R.id.titleText)).setText("搜索");
        searchEditText = (EditText) findViewById(R.id.et_searchtext);
        searchEditText.setText(keyWord);
        searchEditText.setSelection(keyWord.length());
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                keyWord = String.valueOf(charSequence);
//                mHint.setText("搜索结果");
//                mHint.setTextColor(getResources().getColor(R.color.bbb_color));
                if ("".equals(keyWord)) {
                    //  Toast.makeText(getActivity(),"请输入搜索关键字",Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    doSearchQuery(keyWord);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        orderTextView = (AppCompatTextView) findViewById(R.id.orderTextView);
        saleTextView = (AppCompatTextView) findViewById(R.id.saleTextView);
        screenTextView = (AppCompatTextView) findViewById(R.id.screenTextView);
        orderLinearLayout = (LinearLayoutCompat) findViewById(R.id.orderLinearLayout);
        screenRelativeLayout = (RelativeLayout) findViewById(R.id.screenRelativeLayout);
        orderCompTextView = (AppCompatTextView) findViewById(R.id.orderCompTextView);
        orderHighTextView = (AppCompatTextView) findViewById(R.id.orderHighTextView);
        orderLowTextView = (AppCompatTextView) findViewById(R.id.orderLowTextView);
        orderHotTextView = (AppCompatTextView) findViewById(R.id.orderHotTextView);
        priceFromEditText = (AppCompatEditText) findViewById(R.id.priceFromEditText);
        priceToEditText = (AppCompatEditText) findViewById(R.id.priceToEditText);
        confirmTextView = (AppCompatTextView) findViewById(R.id.confirmTextView);
        mainPullRefreshView = (GridView) findViewById(R.id.mainPullRefreshView);
        mAdapter = new ImageShopAdapter(getActivity(), mainArrayList);
        mainPullRefreshView.setAdapter(mAdapter);

        orderTextView.setOnClickListener(this);
        screenTextView.setOnClickListener(this);
        saleTextView.setOnClickListener(this);
    }

    public void initData() {
        doSearchQuery(keyWord);
    }

    public void doSearchQuery(String keyWord) {
        if (keyWord != null && keyWord.length()>0) {
            Set<String> strings = (Set<String>) SharePreferenceUtils.readObject(this, "SEARCH_HISTORY");
            if (strings == null) {
                strings = new HashSet<>();
            }
            strings.add(keyWord);
            SharePreferenceUtils.saveObject(this, "SEARCH_HISTORY", (Serializable) strings);
        }
        JSONObject param = new JSONObject();
        try {
            param.put("limit",100);
            param.put("page",0);
            param.put("title",keyWord);
            param.put("sign",sign);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.searchShopLists(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                mainArrayList.clear();
                JSONArray array = null;
                JSONObject jsonObject = null;
                try {
                    jsonObject = data.getJSONObject("data");
                    array = jsonObject.getJSONArray("records");
                    for (int i = 0; i < array.length(); i++) {
                        ShopBean bean = new Gson().fromJson(array.getJSONObject(i).toString(), ShopBean.class);
                        mainArrayList.add(bean);
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });
    }

    public void initEven() {
    }

    @Override
    public void onActivityResult(int req, int res, Intent intent) {
        super.onActivityResult(req, res, intent);
        if (res == RESULT_OK) {

        }
    }

    //自定义方法

    private void getGoods() {

    }

    private void addCart(String goodsId) {

    }

    public void onSeachCancel(View v) {
        keyWord = "";
        searchEditText.setText(keyWord);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.orderTextView:
//                screenRelativeLayout.setVisibility(View.GONE);
//                screenTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
//                if (orderLinearLayout.getVisibility() == View.VISIBLE) {
//                    orderLinearLayout.setVisibility(View.GONE);
//                    orderTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
//                } else {
//                    orderLinearLayout.setVisibility(View.VISIBLE);
//                    orderTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_up, 0);
//                }
                sign = 0;
                doSearchQuery(keyWord);
                break;

            case R.id.screenTextView:
//                orderLinearLayout.setVisibility(View.GONE);
//                orderTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
//                if (screenRelativeLayout.getVisibility() == View.VISIBLE) {
//                    screenRelativeLayout.setVisibility(View.GONE);
//                } else {
//                    screenRelativeLayout.setVisibility(View.VISIBLE);
//                }
                if (sign == 2) {
                    screenTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_up, 0);
                    sign = 3;
                } else {
                    screenTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down, 0);
                    sign = 2;
                }
                doSearchQuery(keyWord);
                break;

            case R.id.saleTextView:
                sign = 1;
                doSearchQuery(keyWord);
                break;
        }
    }
}
